package com.git.riskwarning;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.git.riskwarning.dao.RiskDaoImpl;

public class RiskWarnServlet extends HttpServlet{

	/***/
	private static final long serialVersionUID = 1L;
	
	private RiskDaoImpl riskDaoImpl;
	
	public RiskWarnServlet() {
		riskDaoImpl = new RiskDaoImpl();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String method = req.getParameter("method");
		if("selectRiskWarn".equals(method)){
			this.selectRiskWarn(req, resp);
			return;
		}else if("saveRiskWarn".equals(method)){
			this.saveRiskWarn(req, resp);
			return;
		}else if("createRiskWarn".equals(method)){
			this.createRiskWarn(req, resp);
			return;
		}else if("addRiskWarn".equals(method)){
			this.addRiskWarn(req, resp);
			return;
		}else if("delRiskWarn".equals(method)){
			this.delRiskWarn(req, resp);
			return;
		}else if("editRiskWarn".equals(method)){
			this.editRiskWarn(req, resp);
			return;
		}else if("updateRiskWarn".equals(method)){
			this.updateRiskWarn(req, resp);
			return;
		}else{
			List<RiskWarnImpl> riskList = riskDaoImpl.findMarkSet();
			req.setAttribute("riskList", riskList);
			
			req.getRequestDispatcher("/jsp/risk/selectRiskWarn.jsp").forward(req, resp);
		}
		
	}
	
	protected void selectRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		RiskWarnImpl rw = new  RiskWarnImpl();
		rw.setMarkCode(req.getParameter("markCode"));
		rw.setMarkName(req.getParameter("markName"));
		rw.setOrgCode(req.getParameter("orgCode"));
		rw.setBasicPhase(req.getParameter("basicPhase"));
		rw.setGatherFre(req.getParameter("gatherFre"));
		rw.settDate(req.getParameter("tDate"));
		
		List<RiskWarnImpl> riskList = riskDaoImpl.findMarkSet(rw);
		req.setAttribute("riskList", riskList);
		
		req.getRequestDispatcher("/jsp/risk/selectRiskWarn.jsp").forward(req, resp);
		
	}
	
	protected void createRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String markCode = req.getParameter("markCode");
		String orgCode = req.getParameter("orgCode");
		
		List<RiskWarnImpl> riskList = riskDaoImpl.findRiskWarn(markCode, orgCode);
		req.setAttribute("riskList", riskList);
		req.setAttribute("markCode", markCode);
		req.setAttribute("orgCode", orgCode);
		
		req.getRequestDispatcher("/jsp/risk/saveRiskWarn.jsp").forward(req, resp);
		
	}	
	
	protected void saveRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		RiskWarnImpl rw = (RiskWarnImpl) req.getSession().getAttribute("riskWarnImpl");
		rw.setChangePhase(req.getParameter("changePhase"));
		rw.setFirstContent(req.getParameter("firstContent").trim());
		rw.setSendUser(req.getParameter("sendUser"));
		rw.setAlertInterval(req.getParameter("alertInterval"));
		rw.setBulletin(req.getParameter("bulletin"));
		
		riskDaoImpl.saveRiskWarn(rw);
		
		req.getRequestDispatcher("/RiskWarnServlet?method=createRiskWarn&markCode="+rw.getMarkCode()+"&orgCode="+rw.getOrgCode()+"").forward(req, resp);
		
	}
	
	protected void updateRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		RiskWarnImpl rw = (RiskWarnImpl) req.getSession().getAttribute("riskWarnImpl");
		rw.setChangePhase(req.getParameter("changePhase"));
		rw.setFirstContent(req.getParameter("firstContent").trim());
		rw.setSendUser(req.getParameter("sendUser"));
		rw.setBulletin(req.getParameter("bulletin"));
		rw.setOid(Long.valueOf(req.getParameter("oid")));
		riskDaoImpl.updateRiskWarn(rw);
		
		req.getRequestDispatcher("/RiskWarnServlet?method=createRiskWarn&markCode="+rw.getMarkCode()+"&orgCode="+rw.getOrgCode()+"").forward(req, resp);
		
	}
	
	protected void editRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		long oid = Long.valueOf(req.getParameter("oid").toString());
		RiskWarnImpl rw =  riskDaoImpl.findRiskWarn(oid);
		req.getSession().setAttribute("riskWarnImpl", rw);
		req.getRequestDispatcher("/jsp/risk/editRiskWarn.jsp").forward(req, resp);
		
	}
	
	
	protected void delRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String markCode = req.getParameter("markCode");
		String orgCode = req.getParameter("orgCode");
		long oid = Long.valueOf(req.getParameter("oid").toString());
		riskDaoImpl.delRiskWarn(oid);
		req.getRequestDispatcher("/RiskWarnServlet?method=createRiskWarn&markCode="+markCode+"&orgCode="+orgCode).forward(req, resp);
	}
	
	protected void addRiskWarn(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String markCode = req.getParameter("markCode");
		String orgCode = req.getParameter("orgCode");
		
		RiskWarnImpl rw = riskDaoImpl.getRiskWarnImpl(markCode, orgCode);
		
		req.getSession().setAttribute("riskWarnImpl", rw);
		
		req.getRequestDispatcher("/jsp/risk/addRiskWarn.jsp").forward(req, resp);
	}
	
	
}
